package cn.xiehangs;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.utils.Null;

public class qi_zi extends Actor  {
    static int move_step_pool;//共享变量，用于判断哪种颜色的棋子可以移动

    static boolean is_move_pool=true;//共享变量，只有一个棋子可以移动
    float x, y;//棋子的坐标
    int width, height;//棋子的宽高
    String type;//棋子的名字和类型
    int id;//棋子的id
    Texture qi_zi_texture;//棋子的纹理区域
    Texture is_touched,no_touched,win_texture;//棋子的纹理区域
    float []move_x=new float[10];//棋子的移动坐标
    float []move_y=new float[10];//棋子的移动坐标
    int i=0;
    boolean is_move=false;//是否在移动
    boolean is_end=false;//是否到达终点
    boolean is_win=false;//是否胜利
    qi_zi_step s=new qi_zi_step();
    boolean flag_s=false;
    int step=-1;//表示棋子在起点
    聊天框 聊天框_1;
    public qi_zi(String type, int id) {
        this.x = s.get_x(id,this.step);
        this.y = s.get_y(id,this.step);
        this.type = type;//初始化棋子的类型
        this.id = id;//初始化棋子的id
        this.qi_zi_texture=new Texture(Gdx.files.internal("qi_zi/"+type+".png"));
        this.is_touched=new Texture(Gdx.files.internal("qi_zi/"+type+"_touched.png"));
        this.no_touched=new Texture(Gdx.files.internal("qi_zi/"+type+".png"));
        this.win_texture=new Texture(Gdx.files.internal("qi_zi/win.png"));
        for(int j=0;j<10;j++){
            this.move_x[j]=-1;
            this.move_y[j]=-1;
        }
        setTouchable(Touchable.disabled);
    }
    public qi_zi(String type, int id,Texture texture) {
        this.x = s.get_x(id,this.step);
        this.y = s.get_y(id,this.step);
        this.type = type;//初始化棋子的类型
        this.id = id;//初始化棋子的id
        this.qi_zi_texture=texture;
        for(int j=0;j<10;j++){
            this.move_x[j]=-1;
            this.move_y[j]=-1;
        }
        setTouchable(Touchable.disabled);
    }
    public void set聊天框(聊天框 聊天框_1){
        this.聊天框_1=聊天框_1;
    }
    void move(float []move_x,float []move_y){//依据队列前进
        //发送开始移动消息

        for(int j=0;j<10;j++){
            this.move_x[j]=-1;
            this.move_y[j]=-1;
        }
        for (int j=0;j<move_x.length;j++){
            this.move_x[j]=move_x[j];
            this.move_y[j]=move_y[j];
        }
        is_move=true;
    }
    void move_step(int step_nember){//依据步数前进
        boolean is_special=false;
        qi_pan_wz.leave(this.id,this.step);
        for(int j=0;j<10;j++){
            this.move_x[j]=-1;
            this.move_y[j]=-1;
        }
        int j,j1=0;
        for ( j=1;j<=step_nember;j++,j1++){
            //发送开始移动消息
            if(step+j>56){
                is_special=true;
                break;
            }
            if(聊天框_1!=null&&flag_s) {
                 聊天框_1.add("棋子" + this.id + "前进：" + j);
            }
            this.move_x[j1]=s.get_x(this.id,this.step+j);
            this.move_y[j1]=s.get_y(this.id,this.step+j);
        }
        if (is_special){
            int k,k1=0;
            for ( k=1;k<=step_nember-(j-1);k++,k1++)
            {
                this.move_x[j1+k1]=s.get_x(this.id,56-k);
                this.move_y[j1+k1]=s.get_y(this.id,56-k);
            }
            j1=j1+k1;
            step_nember=j-k;
        }
        this.step+=step_nember;
        if(step%4==2&&step<50){
            //判断是不是特殊点
            if(step==18)
            {
                this.move_x[j1]=s.get_x(this.id,step+12);
                this.move_y[j1]=s.get_y(this.id,step+12);
                j1++;
                this.step+=12;
            }
            this.move_x[j1]=s.get_x(this.id,step+4);
            this.move_y[j1]=s.get_y(this.id,step+4);
            j1++;
            this.step+=4;
        }
        if(step==18)
        {
            this.move_x[j1]=s.get_x(this.id,step+12);
            this.move_y[j1]=s.get_y(this.id,step+12);
            j1++;
            this.step+=12;
        }
        if(step==56)
        {
            //发送结束消息
            this.move_x[j1]=s.get_x(this.id,-1);
            this.move_y[j1]=s.get_y(this.id,-1);
            this.step=-1;
            is_end = true;
        }
        is_move=true;
    }

    public void back(){
        for(int j=0;j<10;j++){
            this.move_x[j]=-1;
            this.move_y[j]=-1;
        }
        this.move_x[0] = s.get_x(this.id,-1);
        this.move_y[0] = s.get_y(this.id,-1);
        is_move=true;
        this.step=-1;
    }

    @Override
    public @Null Actor hit(float x, float y, boolean touchable){
        if (qi_zi_texture == null || move_step_pool!=this.id/4&&!is_end) {
            qi_zi_texture=no_touched;
            return null;
        }
        if (!isVisible()) return null;
        float x_1=  (x-this.x);
        float y_1=  (y-this.y);
        if(x_1>0&&y_1>0&&x_1<qi_zi_texture.getWidth()&&y_1<qi_zi_texture.getHeight()&&!is_end){
            qi_zi_texture=is_touched;
            return this;
        }
        else if(!is_end){
            qi_zi_texture=no_touched;
            return null;
        }
        else {
            return null;
        }

//        return x >= 0 && x < width && y >= 0 && y < height ? this : null;
    }


    public int move_action(int move_step){
        if(move_step==-1){
            if (聊天框_1!=null) {
                聊天框_1.add("等待对方操作");
            }
            return this.id/4;
        }
        if (is_end)
        {
            if (聊天框_1!=null) {
                聊天框_1.add("棋子已经到达终点并返回，请选择另外一个棋子");
            }
            return this.id/4;
        }
        //打印棋子的id和步数

        if (move_step==6)
        {//如果是6
            if(step==-1){//如果还没有起飞
                float[]move_x_1={s.get_x(this.id,0)};
                float[]move_y_1={s.get_y(this.id,0)};
                step=0;
                move(move_x_1,move_y_1);
                if(聊天框_1!=null) {
                    聊天框_1.add("棋子" + this.id + "起飞");
                }
                return this.id/4;
            }
            else {//如果已经起飞
                this.move_step(6);
                if(聊天框_1!=null) {
                    聊天框_1.add("棋子" + this.id + "前进：" + move_step);
                }
                return this.id/4;
            }
        }
        else {//如果不是6
            if(step==-1){//如果还没有起飞
                int next=this.id/4+1;
                if (next==4)
                    next=0;
                is_move_pool=true;
                if(聊天框_1!=null) {
                    聊天框_1.add("棋子" + this.id + "无法前进");
                }
                return next;
            }
            else {//如果已经起飞
                this.move_step(move_step);
                int next=this.id/4+1;
                if (next==4)
                    next=0;
                if(聊天框_1!=null) {
                    聊天框_1.add("棋子" + this.id + "前进：" + move_step);
                }
                return next;
            }
        }
    }

    @Override
    public void act(float delta) {
        super.act(delta);
        if (is_move){//移动逻辑
            if (move_x[i]-10>x){
                x+=10;
            }
            if (move_x[i]+10<x){
                x-=10;
            }
            if (move_y[i]-10>y){
                y+=10;
            }
            if (move_y[i]+10<y){
                y-=10;
            }
            if (move_x[i]>x){
                x+=1;
            }
            if (move_x[i]<x){
                x-=1;
            }
            if (move_y[i]>y){
                y+=1;
            }
            if (move_y[i]<y){
                y-=1;
            }
            if (move_x[i]==x&&move_y[i]==y){
                i++;
                if (move_x[i]<0){
                    is_move=false;
                    i=0;
                    qi_pan_wz.pan_daun(this.id,this.step);
                    is_move_pool=true;//移动结束,可以移动下一个棋子
                    if (is_end==true){
                        qi_zi_texture=win_texture;
                    }
                }
            }
        }
        super.act(delta);

        // 处理点击和触摸事件
        if (Gdx.input.justTouched()) {
            // 获取触摸点坐标
            float touchX = Gdx.input.getX();
            float touchY = Gdx.input.getY();
            touchY = Gdx.graphics.getHeight() - 1 - touchY;

            // 判断触摸点是否在Actor范围内
            if (hit(touchX,touchY, true) != null) {
                // 触发点击事件
                onClick();
            }
        }
    }


    private void onClick() {
        this.qi_zi_texture=no_touched;
        //点击事件
        if(move_step_pool==this.id/4&&is_move_pool==true){
            is_move_pool=false;
            move_step_pool=move_action(tou_zi.get_dian_shu());
        }
        else {
            System.out.println("请等待对方操作");
        }
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        if (qi_zi_texture == null || !isVisible()) {
            return;
        }
        batch.draw(qi_zi_texture, x, y);
    }
    public static void setMove_step_pool(int x){
        move_step_pool=x;
    }
    public static int getMove_step_pool(){
        return move_step_pool;
    }
    public void chu_shi_hua(){
        this.is_end=false;
        this.is_move=false;
        this.step=-1;
        i=0;
        this.x = s.get_x(id,this.step);
        this.y = s.get_y(id,this.step);
        this.qi_zi_texture=no_touched;
    }
}
